-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
m.(cdo|tnm).download: Replace requests with urllib #977
base: grass8
Are you sure you want to change the base?
Conversation
outf.write(inf.read()) | ||
except urllib.error.HTTPError as e: | ||
grass.warning( | ||
_("Failed to download %s with status code %d") % (filename, e.code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.format(filename=..., code=...)
is preferred over C-like %s %d
. The new code does not have to be outdated just because the surrounding code is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought C-like formats would be better for cross-language translations, but anyway, we already have Python formats, so not a big deal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We definitively use .format
in new Python code. So far, we weighted the clarity of code and possible order changes in translations over translation reuse. Plus these are not really translated at this point, no? So it is only the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wenzeslaus Shouldn't we prefer f-strings (better readability and shorter) in that case when the minimum Python version we support is 3.8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't adapt the CI yet, but for the main branch, the minimum supported version agreed upon is 3.9. So you are free
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this was this GRASS repo, not the GRASS addons repo. For addons I don't know, but hope to follow the main grass repo to allow sharing similar maintenance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule now is: f-strings for plain strings, but _("...").format(key=...)
for translatable ones. (f-strings are overall better and recommended by linters but don't work with translations.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def urlopen(url): | ||
url = url.replace(" ", "%20") | ||
return urllib.request.urlopen(url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is probably encode-decode function which would do this in more general way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found url = urllib.parse.quote(url, safe=":/?=&")
, but I liked the above simpler version. Any better suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually surprised that I couldn't find any general URL encode/decode functions that can simply take full URLs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh BTW, TNM only complained about spaces in URL. That was another reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I don't have a better suggestion. I think a documentation would clarify the existence of the function or a separate function encode_spaces()
or encode_X_for_Y()
as there seems to be some specificity to this particular case.
This PR fixes #976.